from fastapi import APIRouter

from app.database.postgre import SessionDep
from app.database.models import App
from app.models import responses
from app.models.common import AccountIdJSON, AppIdParams
from app.models.super import (AccountsListParams, CreateAccountJSON, DisableAccountJSON, UpdateAccountJSON,
                              ModifyPasswordJSON, AccountAppListParams, UpdateAccountAppJSON, BatchDeleteAccountsJSON)
from app.services import accounts, super

router = APIRouter(
    prefix='/api/v1/super',
    tags=['backend', 'super']
)


@router.get('/accounts/list')
async def accounts_list(
        account: accounts.LoggedInAccount,
        query_params: AccountsListParams,
        session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.get_accounts_list(query_params, session)
    return response


@router.post('/accounts/create')
async def create_account(account: accounts.LoggedInAccount, json_data: CreateAccountJSON, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.create_new_account(account, json_data, session)
    return response


@router.post('/accounts/disable')
async def disable_account(account: accounts.LoggedInAccount, json_data: DisableAccountJSON, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.disable_account(account, json_data, session)
    return response


@router.post('/accounts/update')
async def update_account(account: accounts.LoggedInAccount, json_data: UpdateAccountJSON, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.update_account(account, json_data, session)
    return response


@router.post('/accounts/modify_password')
async def modify_password(account: accounts.LoggedInAccount, json_data: ModifyPasswordJSON, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.modify_password_by_super(json_data, session)
    return response


@router.post('/accounts/delete')
async def delete_account(account: accounts.LoggedInAccount, json_data: AccountIdJSON, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.delete_account_by_id(account, json_data, session)
    return response


@router.post('/accounts/batch_delete')
async def batch_delete_accounts(account: accounts.LoggedInAccount, json_data: BatchDeleteAccountsJSON,
                                session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.delete_account_by_ids(account, json_data, session)
    return response


@router.post('/accounts/clear')
async def clear_accounts(account: accounts.LoggedInAccount, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.clear_all_account(account, session)
    return response


@router.get('/accounts/apps/list')
async def account_apps_list(
        account: accounts.LoggedInAccount,
        query_params: AccountAppListParams,
        session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.get_account_apps(query_params, session)
    return response


@router.get('/apps/info')
async def app_info(account: accounts.LoggedInAccount, query_params: AppIdParams, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    # 获取被查询的应用
    app = session.get(App, query_params.app_id)
    if not app:
        return responses.Response303()

    return responses.SuccessResponse(data=app.model_dump())


@router.post('/apps/update')
async def update_app(account: accounts.LoggedInAccount, json_data: UpdateAccountAppJSON, session: SessionDep):
    # 验证超级管理员权限
    if not account.has_super_permission:
        return responses.Response202()

    response = await super.update_app(json_data, session)
    return response
